package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.BeforeDehydrationMapper;
import com.zhijian.medical.entity.po.BeforeDehydration;
import com.zhijian.medical.entity.vo.request.BeforeDehydrationInsideSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.BeforeDehydrationSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.BeforeDehydrationSearchVo;
import com.zhijian.medical.entity.vo.response.BeforeDehydrationListVo;
import com.zhijian.medical.entity.vo.response.BeforeDehydrationVo;
import com.zhijian.medical.enums.DehydrationEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.BeforeDehydrationService;
import com.zhijian.medical.service.ExperimentProcessSampleService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.NumUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 脱水前的样本放置容器表业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-11-08 16:27:02
 */
@Slf4j
@Service
public class BeforeDehydrationServiceImpl implements BeforeDehydrationService {

    @Autowired
    private BeforeDehydrationMapper mapper;

    @Autowired
    private ExperimentProcessSampleService experimentProcessSampleService;

    @Override
    public BeforeDehydration selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public BeforeDehydrationVo selectVoById(Long id) {
        BeforeDehydrationVo beforeDehydrationVo = mapper.selectVoById(id);
        if (Objects.isNull(beforeDehydrationVo)) {
            return null;
        }
        String sampleIds = beforeDehydrationVo.getSampleIds();
        if (StringUtils.isBlank(sampleIds)) {
            return beforeDehydrationVo;
        }
        //循环样本的分子病理号，查出所有的样本VO
        beforeDehydrationVo.setSampleList(Arrays.stream(sampleIds.split(",")).collect(Collectors.toList()));
        return beforeDehydrationVo;
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(BeforeDehydration beforeDehydration) {
        Date now = new Date();
        beforeDehydration.setCreateTime(now);
        beforeDehydration.setUpdateTime(now);
        return mapper.insert(beforeDehydration) > 0 ? beforeDehydration.getId() : 0L;
    }

    @Override
    public boolean update(BeforeDehydration beforeDehydration) {
        Date now = new Date();
        beforeDehydration.setUpdateTime(now);
        return mapper.updateByPrimaryKey(beforeDehydration) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(BeforeDehydration record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(BeforeDehydrationSearchVo beforeDehydrationSearchVo) {
        return mapper.count(beforeDehydrationSearchVo);
    }

    @Override
    public List<BeforeDehydrationListVo> selectList(BeforeDehydrationSearchVo beforeDehydrationSearchVo) {
        return mapper.selectList(beforeDehydrationSearchVo);
    }

    @Override
    public Page<BeforeDehydrationListVo> selectPage(BeforeDehydrationSearchVo beforeDehydrationSearchVo) {
        //先直接查总数
        int count = count(beforeDehydrationSearchVo);
        //分页
        Page<BeforeDehydrationListVo> page = new Page<>(true, count, beforeDehydrationSearchVo.getPageNum(), beforeDehydrationSearchVo.getPageSize());
        beforeDehydrationSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<BeforeDehydrationListVo> responseVos = this.selectList(beforeDehydrationSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<BeforeDehydration> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public boolean save(BeforeDehydrationSaveOrUpdateVo beforeDehydrationSaveOrUpdateVo) {
        AssertUtil.check(beforeDehydrationSaveOrUpdateVo.getBeginSampleNumber() >= beforeDehydrationSaveOrUpdateVo.getEndSampleNumber(), "开始样本号要小于结束样本号");
        //找出这俩样本，中间的所有样本
        Integer[] range = NumUtil.range(beforeDehydrationSaveOrUpdateVo.getBeginSampleNumber(), beforeDehydrationSaveOrUpdateVo.getEndSampleNumber(), 1);

        BeforeDehydration beforeDehydration = new BeforeDehydration();
        beforeDehydration.setContainerNumber(beforeDehydrationSaveOrUpdateVo.getContainerNumber());
        beforeDehydration.setSampleIds(String.join(",", Arrays.stream(range).map(String::valueOf).collect(Collectors.toList())));
        //默认待脱水
        beforeDehydration.setStatus(DehydrationEnum.WAIT_DEHYDRATION.getCode().byteValue());
        beforeDehydration.setOperateUserId(CurrentUserHolder.getUser().getId());
        String processNumber = String.format("Deh%s", DateUtil.format(new Date(), new SimpleDateFormat("yyyyMMddHHmmss")));
        beforeDehydration.setProcessNumber(processNumber);
        return this.insert(beforeDehydration) != 0;
    }

    @Override
    public boolean insideSave(BeforeDehydrationInsideSaveOrUpdateVo beforeDehydrationInsideSaveOrUpdateVo) {
        List<String> sampleMoleculePathologyNumbers = beforeDehydrationInsideSaveOrUpdateVo.getSampleMoleculePathologyNumbers();
        BeforeDehydration beforeDehydration = this.selectById(beforeDehydrationInsideSaveOrUpdateVo.getId());
        return mapper.updateByPrimaryKeySelective(BeforeDehydration.builder()
                .id(beforeDehydration.getId())
                .sampleIds(String.join(",", sampleMoleculePathologyNumbers))
                .build()) != 0;
    }

}